import { Module } from '@nestjs/common';
import { JwtModule as NestJwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtStrategy } from './jwt.strategy';
import { JwtService } from './jwt.service';
import { TokenBlacklistService } from '../common/services/token-blacklist.service';
import { RedisUtil } from '../../common/utils';

/**
 * JWT 认证模块
 *
 * 提供：
 * - JWT Token 生成和验证
 * - Passport JWT 策略
 * - JWT 认证守卫
 * - Token 黑名单（登出失效）
 */
@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt' }),
    NestJwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        secret: configService.get('JWT_SECRET') || '123456',
        signOptions: {
          expiresIn: configService.get('JWT_EXPIRES_IN') || '7d',
        },
      }),
    }),
  ],
  providers: [JwtService, JwtStrategy, TokenBlacklistService, RedisUtil],
  exports: [JwtService, TokenBlacklistService, NestJwtModule],
})
export class JwtModule {}

